package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Check;
import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.Rent;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.CarService;
import com.hzdl.car.service.CheckService;
import com.hzdl.car.service.CustomerService;
import com.hzdl.car.service.RentService;
import com.hzdl.car.util.Common;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.util.ResultObj;
import com.hzdl.car.util.SysConstast;
import com.hzdl.car.vo.CheckVo;
import com.hzdl.car.vo.RentVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("check")
public class CheckController {
    @Resource
    private CheckService checkService;
    @Resource
    private RentService rentService;
    @Resource
    private CustomerService customerService;
    @Resource
    private CarService carService;
    @RequestMapping("findCheck")
    public HzdlResultJson findCheck(RentVo rentVo, HttpSession session){
        try {
            Rent rent=rentService.findCarNById(rentVo);
            if(rent==null){
                return HzdlResultJson.error(1,"该订单号不存在");
            }
            Object o = session.getAttribute("user");
            String name = "";
            if(o != null){
                User user = (User)o;
                name = user.getRealName();
            }
            Map<String,Object> map=new HashMap<>();
            map.put("rent",rent);
            map.put("customer",customerService.findCustomerById(rent.getIdentity()).get(0));
            map.put("car",carService.findCarByNum(rent.getCarNumber()));
            map.put("checkId", Common.createRandomStringUseTime("JC"));
            map.put("openName",name);
            return HzdlResultJson.ok(map);

        }catch (Exception e){
            return HzdlResultJson.error(-1,e.getMessage());
        }
    }
    @RequestMapping("addCheck")
    public ResultObj addCheck(CheckVo checkVo){
        try {

           checkService.addCheck(checkVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("findCheckBy")
    public HzdlResultJson findCheck(CheckVo checkVo){
        try {
            PageInfo<Check> pageInfo=checkService.findCheck(checkVo);
            return HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
        }catch (Exception e){
            return HzdlResultJson.error(-1,e.getMessage());
        }
    }
    @RequestMapping("delCheck")
    public ResultObj delCheck(CheckVo checkVo){
        try {
            checkService.delCheck(checkVo);
            return ResultObj.DELETE_SUCCESS;

        }catch (Exception e){
            return  ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("updateCheck")
    public ResultObj updateCheck(CheckVo checkVo){
        try {
            checkService.updateCheck(checkVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }
}
